import turtle
import random
import math
import time

# 设置中文字体支持
turtle.rcParams = {"font.family": ["SimHei", "WenQuanYi Micro Hei", "Heiti TC"]}

class SecretGarden:
    def __init__(self):
        # 创建画布
        self.screen = turtle.Screen()
        self.screen.title("秘密花园")
        self.screen.setup(width=1000, height=800)
        self.screen.bgcolor("#87CEEB")  # 天空蓝色
        
        # 创建画笔
        self.pen = turtle.Turtle()
        self.pen.speed(0)  # 最快速度
        self.pen.hideturtle()
        
        # 花园元素数量
        self.num_flowers = 30
        self.num_trees = 10
        self.num_butterflies = 5
        
        # 颜色方案
        self.grass_colors = ["#7CFC00", "#228B22", "#32CD32"]
        self.flower_colors = ["#FF69B4", "#FF1493", "#FFD700", "#FF6347", "#9370DB"]
        self.tree_colors = ["#8B4513", "#2F4F4F"]
        
        # 开始绘制
        self.draw_garden()
        
        # 保持窗口打开
        self.screen.exitonclick()
    
    def draw_ground(self):
        """绘制地面"""
        self.pen.penup()
        self.pen.goto(-500, -200)
        self.pen.pendown()
        self.pen.color("#228B22")
        self.pen.begin_fill()
        for _ in range(2):
            self.pen.forward(1000)
            self.pen.right(90)
            self.pen.forward(400)
            self.pen.right(90)
        self.pen.end_fill()
        
        # 绘制一些草丛细节
        self.pen.penup()
        self.pen.goto(-500, -200)
        self.pen.pendown()
        for x in range(-500, 500, 30):
            self.pen.penup()
            self.pen.goto(x, -200)
            self.pen.pendown()
            self.pen.color(random.choice(self.grass_colors))
            height = random.randint(10, 30)
            self.pen.setheading(90)
            self.pen.forward(height)
            self.pen.backward(height)
    
    def draw_flower(self, x, y):
        """在指定位置绘制一朵花"""
        # 花茎
        self.pen.penup()
        self.pen.goto(x, y)
        self.pen.pendown()
        self.pen.color("#006400")
        self.pen.setheading(90)
        stem_length = random.randint(30, 80)
        self.pen.forward(stem_length)
        flower_center = (x, y + stem_length)
        
        # 花瓣
        petal_color = random.choice(self.flower_colors)
        self.pen.color(petal_color)
        petal_size = random.randint(10, 20)
        
        for _ in range(12):
            self.pen.begin_fill()
            self.pen.circle(petal_size, 60)
            self.pen.left(120)
            self.pen.circle(petal_size, 60)
            self.pen.end_fill()
            self.pen.right(30)
        
        # 花心
        self.pen.penup()
        self.pen.goto(flower_center)
        self.pen.pendown()
        self.pen.color("#FFD700")
        self.pen.begin_fill()
        self.pen.circle(petal_size / 3)
        self.pen.end_fill()
    
    def draw_tree(self, x, y):
        """在指定位置绘制一棵树"""
        # 树干
        self.pen.penup()
        self.pen.goto(x, y)
        self.pen.pendown()
        self.pen.color(random.choice(self.tree_colors))
        self.pen.setheading(90)
        trunk_height = random.randint(100, 200)
        trunk_width = trunk_height // 10
        self.pen.pensize(trunk_width)
        self.pen.forward(trunk_height)
        crown_center = (x, y + trunk_height)
        
        # 树冠
        self.pen.penup()
        self.pen.goto(crown_center)
        self.pen.pendown()
        self.pen.color("#228B22")
        self.pen.pensize(1)
        crown_size = random.randint(50, 100)
        
        # 绘制多个圆形组成树冠
        for angle in range(0, 360, 60):
            self.pen.penup()
            self.pen.setheading(angle)
            self.pen.forward(crown_size // 2)
            self.pen.pendown()
            self.pen.begin_fill()
            self.pen.circle(crown_size)
            self.pen.end_fill()
            self.pen.penup()
            self.pen.backward(crown_size // 2)
    
    def draw_butterfly(self, x, y):
        """绘制一只蝴蝶并让它飞动"""
        butterfly = turtle.Turtle()
        butterfly.speed(0)
        butterfly.penup()
        butterfly.goto(x, y)
        butterfly.hideturtle()
        
        # 蝴蝶颜色
        color1 = random.choice(["#FF69B4", "#FF1493", "#8A2BE2", "#00CED1"])
        color2 = random.choice(["#FFD700", "#FFFF00", "#ADFF2F", "#FF6347"])
        
        def draw_butterfly_shape():
            butterfly.clear()
            butterfly.penup()
            butterfly.goto(butterfly.xcor(), butterfly.ycor())
            butterfly.pendown()
            
            # 身体
            butterfly.color("black")
            butterfly.pensize(2)
            butterfly.setheading(90)
            butterfly.forward(10)
            
            # 翅膀
            butterfly.penup()
            butterfly.backward(5)
            butterfly.pendown()
            
            # 左翅膀
            butterfly.begin_fill()
            butterfly.color(color1)
            butterfly.setheading(135)
            butterfly.circle(15, 180)
            butterfly.end_fill()
            
            # 右翅膀
            butterfly.begin_fill()
            butterfly.color(color2)
            butterfly.setheading(45)
            butterfly.circle(15, 180)
            butterfly.end_fill()
        
        # 让蝴蝶飞动的动画
        def fly():
            for _ in range(100):
                draw_butterfly_shape()
                dx = random.randint(-5, 5)
                dy = random.randint(-3, 3)
                butterfly.goto(butterfly.xcor() + dx, butterfly.ycor() + dy)
                time.sleep(0.1)
            butterfly.clear()
        
        # 启动蝴蝶飞行动画
        self.screen.ontimer(fly, 100)
    
    def draw_sun(self):
        """绘制太阳"""
        self.pen.penup()
        self.pen.goto(350, 300)
        self.pen.pendown()
        self.pen.color("#FFD700")
        self.pen.begin_fill()
        self.pen.circle(40)
        self.pen.end_fill()
        
        # 阳光
        self.pen.color("#FFA500")
        for _ in range(12):
            self.pen.penup()
            self.pen.goto(350, 300)
            self.pen.pendown()
            self.pen.setheading(_ * 30)
            self.pen.forward(60)
    
    def draw_title(self):
        """绘制标题"""
        self.pen.penup()
        self.pen.goto(0, 320)
        self.pen.pendown()
        self.pen.color("#8B4513")
        self.pen.write("秘密花园", align="center", font=("SimHei", 24, "bold"))
    
    def draw_garden(self):
        """绘制整个花园"""
        # 绘制背景元素
        self.draw_sun()
        self.draw_ground()
        self.draw_title()
        
        # 绘制树木
        for _ in range(self.num_trees):
            x = random.randint(-450, 450)
            y = -200  # 地面上
            self.draw_tree(x, y)
        
        # 绘制花朵
        for _ in range(self.num_flowers):
            x = random.randint(-450, 450)
            y = random.randint(-180, 50)  # 地面上到中等高度
            self.draw_flower(x, y)
        
        # 绘制蝴蝶
        for _ in range(self.num_butterflies):
            x = random.randint(-450, 450)
            y = random.randint(100, 300)  # 空中
            self.draw_butterfly(x, y)

if __name__ == "__main__":
    garden = SecretGarden()
    